import { EventService } from 'src/app/shared/service/event.service';
import { Component, OnInit, Input, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { IonSlides } from '@ionic/angular';
@Component({
  selector: 'app-text-input',
  templateUrl: './text-input.component.html',
  styleUrls: ['./text-input.component.scss'],
})
export class TextInputComponent implements OnInit {
  @Input()  item: any;
  @Input()  superItem: any;
  @Input()  formGroup: FormGroup;
  @Output() output: EventEmitter<any> = new EventEmitter();
  idx: any = 1;
  slideOpts = {
    initialSlide: 0, // 默认显示第几个slide
    speed: 400
  };
  constructor(
    private cd: ChangeDetectorRef,  // 强制更新检测
    private eventService: EventService
  ) { }

  ngOnInit() {
    if(this.item.formType === 'dragSort' && this.item.value == 2) {
      // 第一行数量
      this.item.size = Math.round(this.item.itemList.length / 2);
      if ( this.item.textType == '3') {
        this.sliceData(this.item);
      }
    }
  }
  sliceData(item) {
    if (item.maxSize) {
      item.largeData = [];
      const size = Number(item.maxSize);
      if (item.itemList.slice(0, size).length > 0) {
        item.largeData.push(item.itemList.slice(0, size));
      }
      if (item.itemList.slice(size, size * 2).length > 0) {
        item.largeData.push(item.itemList.slice(size, size * 2));
      }
      if (item.itemList.slice(size * 2, size * 3).length > 0) {
        item.largeData.push(item.itemList.slice(size * 2, size * 3));
      }
      if (item.itemList.slice(size * 3, size * 4).length > 0) {
        item.largeData.push(item.itemList.slice(size * 3, size * 4));
      }
    }
  }
  getprevIndex() {
    this.idx -= 1;
  }

  getnextIndex() {
    this.idx += 1;
  }
  openModalMoreEditInfo(item) {
    if (item.disabled) {
      return;
    }
    this.output.emit({item, fun: "openModalMoreEditInfo" } );
  }
  inputClick(item) {
    this.output.emit({item, fun: "inputClick" } );

  }
  eyeShow(item) {
    if (!item["isPassword"]) {
      item["isPassword"] = 'password';
    }
    if (item.textType === 'password') {
      item.textType = 'text';
    } else {
      item.textType = 'password';
    }
  }
  endIconClick(item) {

    this.output.emit({item, fun: "endIconClick" } );
  }
  deleteValue(item) {

    this.output.emit({item, fun: "deleteValue" } );
  }
  showActionSheet(item) {

    this.output.emit({item, fun: "showActionSheet" } );

  }
  errorChecked(item) {
    this.output.emit({item, fun: "errorChecked" } );

  }
  changeValue(event, item) {
    
    item.value = event.detail.value;

    if (event.detail.value.length > Number(item.maxLength)) {
      if (item.textType === 'Number' || item.textType === 'number') {
        item.value = Number(item.value.toString().slice(0, Number(item.maxLength)));
      } else {
        item.value = item.value.toString().slice(0, Number(item.maxLength));
      }
      
      // this.formGroup.patchValue({ [item.formKey]: item.value });
      this.cd.markForCheck();
      this.cd.detectChanges();
    }
    this.output.emit({event, item, fun: "changeValue" } );

  }
  getMSCode(superitem, item) {
    this.output.emit({superitem, item, fun: "getMSCode" });
  }
  getServicePrice(item, tag) {
    this.output.emit({item, tag, fun: "getServicePrice"});
  }
  ionBlur(item) {
    this.output.emit({item, fun: "ionBlur"});
  }

  // textArea
  clearItemSpaces(event, item) {
    this.output.emit({event, item, fun: "clearItemSpaces" } );

  }
  textAreaClearItemSpaces(event, item) {
    this.output.emit({event, item, fun: "textAreaClearItemSpaces" } );
  }
  setItemDefValue(item) {
    this.output.emit({item, fun: "setItemDefValue" } );
  }
  // switchButton
  changeItemobjValidator(item, formType, i ) {
    this.output.emit({item, formType, i, fun: "changeItemobjValidator" } );
  }
  openPage(link, item?) {
    this.output.emit({link, item, fun: "openPage" } );
  }
  openModalMapPage(item) {
    this.output.emit({item, fun: "openModalMapPage" } );
  }
  switchButtonSystem(item) {
    this.output.emit({item, fun: "switchButtonSystem" } );
  }
  closeTips(tipId, listId) {
    this.output.emit({tipId, listId, fun: "closeTips" } );
  }

  searchLocal(event, item) {
    this.output.emit({ event, item, fun: "searchLocal" } );
  }
  focusInputLocal(item) {
    this.output.emit({item, fun: "focusInputLocal" } );

  }
  searchChangeLocal(event, item) {
    this.output.emit({event, item, fun: "searchChangeLocal" } );
  }
  // passwordType
  passWordIonBlur(item) {
    this.output.emit({item, fun: "passWordIonBlur" } );
  }

  passWordIonFocus(item) {
    this.output.emit({item, fun: "passWordIonFocus" } );
  }
// pickerSelect
  clickOnPickerSelect(item) {
    this.output.emit({item, fun: "clickOnPickerSelect" } );
  }
  pickerSelect(event, item) {
    this.output.emit({event, item, fun: "pickerSelect" } );

  }
  pickerSelectTwo(event, item) {
    this.output.emit({event, item, fun: "pickerSelectTwo" } );

  }

  // date
  getDate(maxOrMin, item) {
    // this.output.emit({maxOrMin, item, fun: "getDate" } );
    if (item.max && item.max === maxOrMin) {

      return item.maxDate = item.maxDate ? item.maxDate : this.getDateTime(maxOrMin);
    } else if (item.min && item.min === maxOrMin) {

      return item.minDate = item.mimDate ? item.minDate : this.getDateTime(maxOrMin);
    }
  }
   getDateTime(date) {
    // ||
    if (!date) {

      return;
    } else if ((typeof date !== "string")) {
      return date;
    }
    if (date.indexOf(' ') > -1) {
      // console.log(new Date(Date.parse(date.replace(/-/g, "/"))));
      return new Date(Date.parse(date.replace(/-/g, "/")));
    } else {
      // console.log(new Date(Date.parse((date + ' ' + '23:59:59').replace(/-/g, "/"))));
      return new Date(Date.parse((date + ' ' + '23:59:59').replace(/-/g, "/")));
    }
  }
  currentValue(item) {
    this.output.emit({ item, fun: "currentValue" } );

  }
  datetime(event, item) {
    this.output.emit({event, item, fun: "datetime" } );
  }
  ionDateChange(event, item) {
    this.output.emit({event, item, fun: "ionDateChange" } );
  }
  timeSlot(event, item, i) {
    this.output.emit({event, item, i, fun: "timeSlot" } );
  }

  paresAddress(item) {
    this.output.emit({ item, fun: "paresAddress" } );
  }
  changeAddress(item) {
    this.output.emit({ item, fun: "changeAddress" } );
  }
  reorder(event, item) {
    item.drag = 1;
    this.output.emit({event, item, fun: "reorder" } );
  }
}
